package tq.com.tqstudent.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.lib.common.base.BaseLazyLoadFragment;
import com.lib.common.utils.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.common.CommonAdapter;
import tq.com.tqstudent.adapter.common.MultiItemTypeAdapter;
import tq.com.tqstudent.adapter.common.ViewHolder;
import tq.com.tqstudent.bean.IndexBean;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.net.ok.StringCallback;
import tq.com.tqstudent.ui.activity.classes.TopicAllActivity;
import tq.com.tqstudent.ui.activity.classes.TopicListActivity;
import tq.com.tqstudent.views.LoginTipsDialog;

/**
 * @Author rames
 * @date 2019/2/28 11:23
 */
public class ClassItemFragment extends BaseLazyLoadFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    private CommonAdapter adapter;
    private int index = 0;
    private boolean isLoaded = false;
    private IndexBean.Subject subject;
    private ArrayList<IndexBean.ChapterBean> chapterList;

    public static ClassItemFragment getInstance(int index, IndexBean.Subject subject) {
        ClassItemFragment fragment = new ClassItemFragment();
        fragment.index = index;
        fragment.subject = subject;
        return fragment;
    }

    @Override
    protected int setView() {
        return R.layout.item_class_fragment;
    }

    @Override
    protected void init(View view) {
        if (isVisible() && isLoaded) {
            return;
        }
        isLoaded = true;
        chapterList = new ArrayList<>();

        ArrayList<IndexBean.ChapterBean> dataList = new ArrayList<>();
        if(null != subject) {
            if (subject.chapter!=null && subject.chapter.size() > 0) {
                chapterList.addAll(subject.chapter);
            }

            if(chapterList.size() > 8){
                for (int i = 0; i < 8; i++) {
                    dataList.add(chapterList.get(i));
                }
            } else {
                dataList.addAll(chapterList);
            }

            dataList.add(new IndexBean.ChapterBean());
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new CommonAdapter<IndexBean.ChapterBean>(getContext(), R.layout.item_vp_class_fragment, dataList) {
            @Override
            protected void convert(ViewHolder holder, IndexBean.ChapterBean bean, int position) {
                if(position == adapter.getDatas().size()-1){
                    holder.setVisible(R.id.more_layout, true);
                    holder.setVisible(R.id.item_layout, false);
                }else{
                    holder.setVisible(R.id.more_layout, false);
                    holder.setVisible(R.id.item_layout, true);
//                    holder.setText(R.id.index_tv, String.valueOf(bean.id));
                    holder.setText(R.id.content_tv, bean.title);
                }
            }
        };
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(!UserInfo.isLogin()){
                    LoginTipsDialog dialog = new LoginTipsDialog(getActivity());
                    dialog.show();
                }else {
                    if (position == adapter.getDatas().size() - 1) {
                        TopicAllActivity.show(getActivity(), subject.subject.id, subject);

                    } else {
                        TopicListActivity.show(getActivity(), subject.chapter.get(position).id);
                    }
                }
            }
        });
        adapter.notifyDataSetChanged();

        emptyLayout.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        isPrepared = true; //设置懒加载
        lazyLoad();
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }

//        refreshLayout.autoRefresh();
    }
}
